<!DOCTYPE html>
<html>
<head>
    <title>添加医生</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }
        .warning{
            border-color: red !important;
        }

    </style>
    <script type="text/javascript">
        // 姓名非空校验
        let doctorNameCheckFlag = false;
        function doctorNameCheck(doctorName){
            if(doctorName == ''){
                doctorNameCheckFlag = false;
                layer.msg("姓名不能为空",function(){})
                // css样式切换
                $("input[name='doctorName']").addClass("warning")
            }else{
                doctorNameCheckFlag = true;
                // css样式切换
                $("input[name='doctorName']").removeClass("warning")
            }
        }
        // 身份证合法性校验
        let dIdCarCheckFlag = false;
        function dIdCarCheck(dIdCar){
            let regx = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
            if( !regx.test(dIdCar) ){
                dIdCarCheckFlag = false;
                layer.msg("身份证不合法",function(){})
                // css样式切换
                $("input[name='dIdCar']").addClass("warning")
            }else{
                // 出生年月
                let year = parseInt(dIdCar.substr(6,4));
                let month = parseInt(dIdCar.substr(10,2));
                let day = parseInt(dIdCar.substr(12,2));
                $("#dBirthday").text(year + "-" + (month<10?"0"+month:month) + "-" + (day<10?"0"+day:day));
                // 计算年龄
                let nowYear = new Date().getFullYear();
                let nowMoth = new Date().getMonth() + 1;
                let nowDay = new Date().getDate();
                if(nowMoth - month > 0){
                    // 年龄即年份相减
                    $("#dAge").text(nowYear - year);
                }else if(nowMoth - month < 0){
                    // 年龄即年份相减再减一
                    $("#dAge").text(nowYear - year - 1);
                }else if(nowDay - day >= 0){
                    // 月分相等, 日相等或更大, 即刚好满岁或不超出一个月 -> 年龄即年份相减
                    $("#dAge").text(nowYear - year);
                }else{
                    // 今年生日没到 -> 年龄即年份相减再减一
                    $("#dAge").text(nowYear - year - 1);
                }
                // 计算男女 第17位奇数是男, 偶数是女
                if( dIdCar.substr(16,1)%2 == 0 ){
                    $("input[name='dSex']").eq(1).prop("checked",true);	// 女
                }else{
                    $("input[name='dSex']").eq(0).prop("checked",true);  // 男
                }
                dIdCarCheckFlag = true;
                $("input[name='dIdCar']").removeClass("warning")
            }
        }
        // 手机号正确性校验
        let dPhoneCheckFlag = false;
        function dPhoneCheck(dPhone){
            let regx = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            if( !regx.test(dPhone) ){
                dPhoneCheckFlag = false;
                layer.msg("手机号不正确",function(){})
                // css样式切换
                $("input[name='dPhone']").addClass("warning")
            }else{
                dPhoneCheckFlag = true;
                // css样式切换
                $("input[name='dPhone']").removeClass("warning")
            }
        }
        // 座机号正确性校验
        let dTelphoneCheckFlag = false;
        function dTelphoneCheck(dTelphone){
            let regx = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            let reg2 = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
            if( !regx.test(dTelphone) && !reg2.test(dTelphone) ){
                dTelphoneCheckFlag = false;
                layer.msg("座机号不正确",function(){})
                // css样式切换
                $("input[name='dTelphone']").addClass("warning")
            }else{
                dTelphoneCheckFlag = true;
                // css样式切换
                $("input[name='dTelphone']").removeClass("warning")
            }
        }
        // 邮箱正确性校验
        let dEmailCheckFlag = false;
        function dEmailCheck(dEmail){
            var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
            if( !reg.test(dEmail) ){
                dEmailCheckFlag = false;
                layer.msg("邮箱不正确",function(){})
                // css样式切换
                $("input[name='dEmail']").addClass("warning")
            }else{
                dEmailCheckFlag = true;
                // css样式切换
                $("input[name='dEmail']").removeClass("warning")
            }
        }
        // 提交按钮的单击事件
        let submit = true;
        function submitfn(){
            // 所有表单校验一遍
            $("input[name='doctorName']").trigger("blur");
            $("input[name='dIdCar']").trigger("blur");
            $("input[name='dPhone']").trigger("blur");
            $("input[name='dTelphone']").trigger("blur");
            $("input[name='dEmail']").trigger("blur");
            let lay = layer.load(0);
            setTimeout(function(){
                if(!doctorNameCheckFlag || !dIdCarCheckFlag || !dPhoneCheckFlag
                    || !dTelphoneCheckFlag || !dEmailCheckFlag || !submit){
                    layer.close(lay);
                    return false;
                }
                // 验证通过
                submit = false;
                let doctorName = $("input[name='doctorName']").val().trim();
                let dIdCar = $("input[name='dIdCar']").val().trim();
                let dPhone = $("input[name='dPhone']").val().trim();
                let dTelphone = $("input[name='dTelphone']").val().trim();
                let dSex = $("input[name='dSex']").val().trim();
                let dBirthday = $("#dBirthday").text();
                let dAge = $("#dAge").text();
                let dEmail = $("input[name='dEmail']").val().trim();
                let dKeshi = $("select[name='dKeshi']").val().trim();
                let dXueli = $("select[name='dXueli']").val().trim();
                let dDesc = $("#dDesc").val().trim();
                $.ajax({
                    type: "POST",
                    url: origin + "/doctor/",
                    data: {doctorName:doctorName,dSex:dSex,dTelphone:dTelphone,dPhone:dPhone,dIdCar:dIdCar,jwtToken:jwtToken
                        ,dDesc:dDesc,dXueli:dXueli,dKeshi:dKeshi,dEmail:dEmail,dAge:dAge,dBirthday:dBirthday},
                    dataType:"json",
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    success: function(data){
                        layer.closeAll();
                        if(data.code==200) {
                            layer.msg("添加成功",{
                                time: 1500
                            }, function () {
                                window.location.href = "index.html?pageNum=" + globalPageNum + "&pageSize=" + globalPageSize;
                            })
                        }else{
                            alert("添加失败！" + data.msg);
                        }
                    }
                });
            }, 200);
        }
        /*返回列表函数*/
        function back() {
            $('#backid').click(function(){
                window.location.href="index.html?pageNum="+globalPageNum+"&pageSize="+globalPageSize;
            });
        }
        $(function () {
            // 监听提交
            $("#btnAdd").click(function () {
                submitfn();
            });
            back();
        });

    </script>
</head>
<body>
    <div id="myForm" class="definewidth m20">
        <table class="table table-bordered table-hover definewidth m10">
            <tr>
                <td width="10%" class="tableleft">姓名</td>
                <td><input onblur="doctorNameCheck(this.value)" type="text" name="doctorName" value=""/></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">身份证号</td>
                <td><input onblur="dIdCarCheck(this.value)" type="text" name="dIdCar" value=""/></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">手机</td>
                <td><input onblur="dPhoneCheck(this.value)" type="text" name="dPhone" value=""/></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">座机</td>
                <td><input onblur="dTelphoneCheck(this.value)" type="text" name="dTelphone" value=""/></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">性别</td>
                <td><input type="radio" readonly onclick="javascript:return false;" name="dSex" value="0" checked/>男&nbsp;&nbsp;&nbsp;<input onclick="javascript:return false;" readonly type="radio" name="dSex" value="1"/>女</td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">出生日期</td>
                <td id="dBirthday"></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">年龄</td>
                <td id="dAge"></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">电子邮箱</td>
                <td><input onblur="dEmailCheck(this.value)" type="text" name="dEmail" value=""/></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">所属科室</td>
                <td><select name="dKeshi">
                        <option>急诊科</option>
                        <option>骨科</option>
                        <option>血液科</option>
                        <option>皮肤科</option>
                        <option>内科</option>
                        <option>外科</option>
                        <option>儿科</option>
                        <option>中医科</option>
                        <option>口腔科</option>
                        <option>眼科</option>
                        <option>麻醉科</option>
                        <option>药剂科</option>
                        <option>放射科</option>
                        <option>手术室</option>
                        <option>输血科</option>
                        <option>理疗科</option>
                    </select></td>
            </tr>
            <tr>
                <td width="10%" class="tableleft">学历</td>
                <td><select name="dXueli">
                        <option>专科</option>
                        <option>本科</option>
                        <option>硕士</option>
                        <option>博士</option>
                        <option>博士后</option>
                </select></td>
            </tr>
	        <tr>
                <td width="10%" class="tableleft">备注</td>
                <td><textarea id="dDesc"></textarea></td>
	        </tr>
            <tr>
                <td colspan="2"><center>
				    <button id="btnAdd" name="btnAdd" class="btn btn-primary" type="button" >保存</button> &nbsp;&nbsp;
                    <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
			    </center></td>
            </tr>
    </table>
</div>
</body>
</html>